package com.infraassets.components
{
	import com.infraassets.skins.notice.NoticePopUpSkin;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.managers.PopUpManager;
	
	import spark.components.Button;
	import spark.components.Group;
	import spark.components.SkinnableContainer;
	import spark.components.supportClasses.TextBase;
	/**
	 * Author : Khamsany Misran
	 * Date   : 20 Dec 2010
	 * Description : A custom component intend to replace the use of mx.alert component. This component
	 *               work is design to work with flex pop up manager.
	 * */
	public class NoticePopUp extends SkinnableContainer
	{
		[SkinPart(required="true")]
		public var titleDisplay:TextBase;
		
		[SkinPart(required="true")]
		public var messageDisplay:TextBase;
		
		[SkinPart(required="true")]
		public var dragArea:Group;
		
		[SkinPart(required="false")]
		public var closeButton:Button;
		
		[SkinPart(required="false")]
		public var okButton:Button;
		
		[SkinPart(required="false")]
		public var cancelButton:Button;
		
		public const OK:String = "ok";
		public const CLOSE:String = "close";
		
		private var _title:String = "";
		private var _message:String = "";
		private var _showCancelButton:Boolean;
		public function NoticePopUp()
		{
			super();
		}
		
		[Bindable]
		[Inspectable(category="Common", type="String")]
		public function get title():String {
			return _title;
		}
		
		public function set title(value:String):void {
			if (_title == value)
				return;
			
			_title = value;
			
			if (titleDisplay)
				titleDisplay.text = value;
		}
		
		[Bindable]
		[Inspectable(category="Common", type="String")]
		public function get message():String {
			return _message;
		}
		
		public function set message(value:String):void{
			if(_message == value)
				return;
			_message = value;
			
			if(messageDisplay)
				messageDisplay.text = value;
		}
		
		public function set showCancelButton(value:Boolean):void {
			_showCancelButton = value;
		}
		
		public function get showCancelButton():Boolean {
			return _showCancelButton;
		}
		
		override protected function partAdded(partName:String, instance:Object):void
		{
			super.partAdded(partName, instance);
			
			if (instance == titleDisplay)
				titleDisplay.text = _title;
			
			if (instance == messageDisplay)
				messageDisplay.text = _message;
			
			if (instance == closeButton)
				closeButton.addEventListener(MouseEvent.CLICK, close);
			
			if (instance == cancelButton){
				cancelButton.addEventListener(MouseEvent.CLICK, close);
				cancelButton.includeInLayout = _showCancelButton;
			}
			
			if (instance  ==  okButton) 
				okButton.addEventListener(MouseEvent.CLICK, ok);
				
			if (instance == dragArea){
				dragArea.addEventListener(MouseEvent.MOUSE_DOWN, drag);
				dragArea.addEventListener(MouseEvent.MOUSE_UP, stop);
			}
			
		}
		
		override protected function partRemoved(partName:String, instance:Object):void
		{
			super.partRemoved(partName, instance);
			
			if (instance == closeButton)
				closeButton.removeEventListener(MouseEvent.CLICK, close);
			
			if (instance == cancelButton)
				cancelButton.removeEventListener(MouseEvent.CLICK, close);
			
			if (instance  ==  okButton) 
				okButton.removeEventListener(MouseEvent.CLICK, ok);
			
			if (instance == dragArea){
				dragArea.removeEventListener(MouseEvent.MOUSE_DOWN, drag);
				dragArea.removeEventListener(MouseEvent.MOUSE_UP, stop);
			}
		}
		
		override public function stylesInitialized():void {
			this.setStyle('skinClass', Class(NoticePopUpSkin));
		} 
		
		protected function ok(event:MouseEvent):void {
			dispatchEvent(new Event(this.OK));
			close();
		}
		
		protected function close(event:MouseEvent = null):void {
			if(event)
				event.stopPropagation();
			dispatchEvent(new Event(this.CLOSE));
			PopUpManager.removePopUp(this);
		}
		
		protected function drag(event:MouseEvent):void {
			this.startDrag();
		}
		
		protected function stop(event:MouseEvent):void {
			this.stopDrag();
		}
		
	}
}